using System.Collections;
using UnityEngine;

public abstract class SynchronizedGameObject : EventCommander
{
	public Renderer _mainRender;

	public int ownerViewId;

	public PhotonView _photonView;

	public PhotonView ownerPhotonView;

	public PhotonTransformView _lerpTransformPhoton;

	private PhotonPlayer _photonPlayer;

	private float initialTeleportParam = 20f;

	protected Renderer mainRender
	{
		get
		{
			if (_mainRender == null)
			{
				if (this == null)
				{
					return null;
				}
				_mainRender = GetComponent<Renderer>();
				if (_mainRender == null)
				{
					_mainRender = GetComponentInChildren<Renderer>();
				}
			}
			return _mainRender;
		}
	}

	public bool isMine
	{
		get
		{
			return settings.offlineMode || photonView == null || photonView.isMine;
		}
	}

	public PhotonView photonView
	{
		get
		{
			if (_photonView == null)
			{
				_photonView = PhotonView.Get(this);
				if (_photonView == null && base.gameObject != null)
				{
					_photonView = base.gameObject.GetComponentInParent<PhotonView>();
				}
			}
			return _photonView;
		}
	}

	public int ID
	{
		get
		{
			if (settings.offlineMode)
			{
				return -1;
			}
			return PhotonNetwork.player.ID;
		}
	}

	public PhotonPlayer photonPlayer
	{
		get
		{
			if (_photonPlayer == null)
			{
				_photonPlayer = PhotonPlayer.Find(ID);
			}
			return _photonPlayer;
		}
	}

	public int ownerId
	{
		get
		{
			if (settings.offlineMode)
			{
				return -1;
			}
			return photonView.ownerId;
		}
		set
		{
			if (!settings.offlineMode)
			{
				photonView.ownershipTransfer = OwnershipOption.Takeover;
				photonView.ownerId = value;
			}
		}
	}

	public int viewId
	{
		get
		{
			if (photonView == null)
			{
				return -1;
			}
			return photonView.viewID;
		}
		set
		{
			if (photonView != null)
			{
				photonView.viewID = value;
				photonView.instantiationId = value;
			}
		}
	}

	public bool visibleForCamera
	{
		get
		{
			return !(mainRender == null) && mainRender.isVisible;
		}
	}

	public virtual void Enable()
	{
	}

	public virtual void Disable()
	{
	}

	public override void Awake()
	{
		base.Awake();
		_lerpTransformPhoton = GetComponent<PhotonTransformView>();
		if (_lerpTransformPhoton != null)
		{
			initialTeleportParam = _lerpTransformPhoton.m_PositionModel.TeleportIfDistanceGreaterThan;
		}
	}

	public void GetControll(SynchronizedGameObject newOwner)
	{
		int newOwnerViewId = newOwner.viewId;
		GetControll(newOwnerViewId);
	}

	public void GetControll(int newOwnerViewId)
	{
		if (!settings.offlineMode)
		{
			if (newOwnerViewId > 0)
			{
				ownerPhotonView = PhotonView.Find(newOwnerViewId);
				ownerViewId = newOwnerViewId;
				ownerId = ownerPhotonView.ownerId;
			}
			else
			{
				ownerViewId = -1;
				ownerPhotonView = null;
			}
		}
	}

	public void RPC(string name, PhotonTargets targets, params object[] parameters)
	{
		if (!settings.offlineMode && photonView != null && isMine)
		{
			photonView.RPC(name, targets, parameters);
		}
	}

	public void ToggleTeleportParam()
	{
		if ((bool)_lerpTransformPhoton)
		{
			StartCoroutine(SetTeleportParamWithDelay());
		}
	}

	public void ToggleTeleportParam(float delay)
	{
		if ((bool)_lerpTransformPhoton)
		{
			StartCoroutine(SetTeleportParamWithDelay(delay));
		}
	}

	private IEnumerator SetTeleportParamWithDelay()
	{
		_lerpTransformPhoton.m_PositionModel.TeleportIfDistanceGreaterThan = 1f;
		yield return new WaitForSeconds(0.25f);
		_lerpTransformPhoton.m_PositionModel.TeleportIfDistanceGreaterThan = initialTeleportParam;
	}

	private IEnumerator SetTeleportParamWithDelay(float delay)
	{
		yield return new WaitForSeconds(0.25f);
		_lerpTransformPhoton.m_PositionModel.TeleportIfDistanceGreaterThan = 1f;
		yield return new WaitForSeconds(delay);
		_lerpTransformPhoton.m_PositionModel.TeleportIfDistanceGreaterThan = initialTeleportParam;
	}
}
